package com.heimo.babyage.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import com.heimo.babyage.R;

public class PrefHelper {
	private static final String TAG = "PrefHelper";
	private static final String PREFS_NAME = "com.heimo.babyage.PrefHelper";

	private static final String PREF_PREFIX_BDAY_KEY = "babyage_bday_";
	private static final String PREF_PREFIX_IMG_KEY = "babyage_img_";
	private static final String PREF_PREFIX_ALPHA_KEY = "babyage_alpha_";
	private static final String PREF_PREFIX_FRAME_KEY = "babyage_frame_";
	private static final String PREF_PREFIX_SPONSOR_KEY = "babyage_has_sponsored";
	
	private static final String PREF_PREFIX_TPATTERN_KEY = "babyage_text_pattern_";
	private static final String PREF_PREFIX_TSIZE_KEY = "babyage_text_size_";
	private static final String PREF_PREFIX_TCOLOR_KEY = "babyage_text_color_";

	private static final String FILENAME = "Avatar";

	// Delete all Preference
	public static void fnDeletePref(Context ctx, int appWidgetId) {
		SharedPreferences.Editor prefs = ctx
				.getSharedPreferences(PREFS_NAME, 0).edit();
		prefs.remove(PREF_PREFIX_BDAY_KEY + appWidgetId);
		prefs.remove(PREF_PREFIX_IMG_KEY + appWidgetId);
		prefs.remove(PREF_PREFIX_ALPHA_KEY + appWidgetId);
		prefs.remove(PREF_PREFIX_SPONSOR_KEY + appWidgetId);
		prefs.remove(PREF_PREFIX_TPATTERN_KEY + appWidgetId);
		prefs.remove(PREF_PREFIX_TSIZE_KEY + appWidgetId);
		prefs.remove(PREF_PREFIX_TCOLOR_KEY + appWidgetId);
		prefs.commit();
		fnDeleteImage(ctx, appWidgetId);
	}

	// Date Preference Start
	public static void fnSaveBDayPref(Context context, int appWidgetId,
			Date date) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putLong(PREF_PREFIX_BDAY_KEY + appWidgetId, date.getTime());
		prefs.commit();
	}

	public static Date fnLoadBDayPref(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		Long time = prefs.getLong(PREF_PREFIX_BDAY_KEY + appWidgetId,
				new Date().getTime());
		Date dt = new Date(time);
		LogHelper.log(TAG, "Pref : " + PREF_PREFIX_BDAY_KEY + appWidgetId
				+ " | " + dt);
		return dt;
	}

	// Date Preference End

	// Image Preference Start
	public static Bitmap fnLoadImage(Context ctx, int appWidgetId) {
		FileInputStream in;
		Bitmap bm = null;
		String fileName = FILENAME + appWidgetId + ".png";
		try {
			ExternalStorageFileHelper esFile = new ExternalStorageFileHelper(
					ctx);
			if (esFile.hasFile(fileName)) {
				in = ctx.openFileInput(fileName);
				bm = BitmapFactory.decodeStream(in);
			}
		} catch (FileNotFoundException e) {
			// Toast.makeText(ctx, "Error Loading Image...", Toast.LENGTH_LONG)
			// .show();
			LogHelper.log(TAG, "Error Load image : " + FILENAME + appWidgetId
					+ ".png");
		}
		if (bm == null) {
			bm = BitmapFactory.decodeResource(ctx.getResources(),
					R.drawable.ic_launcher);
		}
		LogHelper.log(TAG, "Load image : " + FILENAME + appWidgetId + ".png");
		return bm;
	}

	public static void fnSaveImage(Context ctx, int appWidgetId, Bitmap bm) {
		ExternalStorageFileHelper esFile = new ExternalStorageFileHelper(ctx);
		esFile.saveBitmap(FILENAME + appWidgetId + ".png", bm);
		LogHelper.log(TAG, "Save image : " + FILENAME + appWidgetId + ".png");
	}

	public static void fnDeleteImage(Context ctx, int appWidgetId) {
		ExternalStorageFileHelper esFile = new ExternalStorageFileHelper(ctx);
		esFile.delFile(FILENAME + appWidgetId + ".png");
		LogHelper.log(TAG, "Delete image : " + FILENAME + appWidgetId + ".png");
	}

	// Image Preference End

	// Alpha Preference Start
	public static int fnLoadAlphaPref(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		int f = prefs.getInt(PREF_PREFIX_ALPHA_KEY + appWidgetId, 255);
		LogHelper.log(TAG, "Pref : " + PREF_PREFIX_ALPHA_KEY + appWidgetId
				+ " | " + f);
		return f;
	}

	public static void fnSaveAlphaPref(Context context, int appWidgetId, int f) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putInt(PREF_PREFIX_ALPHA_KEY + appWidgetId, f);
		prefs.commit();
	}
	// Alpha Preference End
	
	// Frame Preference Start
	public static int fnLoadFramePref(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		int f = prefs.getInt(PREF_PREFIX_FRAME_KEY + appWidgetId, 0);
		LogHelper.log(TAG, "Pref : " + PREF_PREFIX_FRAME_KEY + appWidgetId
				+ " | " + f);
		return f;
	}
	
	public static void fnSaveFramePref(Context context, int appWidgetId,
			int frame) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putInt(PREF_PREFIX_FRAME_KEY + appWidgetId, frame);
		prefs.commit();
	}
	// Frame Preference End
	
	// Sponsor Preference Start
	public static boolean hasSponsor(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		boolean b = prefs.getBoolean(PREF_PREFIX_SPONSOR_KEY, false);
		LogHelper.log(TAG, "Pref : " + PREF_PREFIX_SPONSOR_KEY + " | " + b);
		return b;
	}

	public static void fnSaveSponsor(Context context) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putBoolean(PREF_PREFIX_SPONSOR_KEY, true);
		prefs.commit();
	}
	// Sponsor Preference End
	
	// Text Pattern Preference Start
	public static int fnLoadTextPatternPref(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		int f = prefs.getInt(PREF_PREFIX_TPATTERN_KEY + appWidgetId, 0);
		LogHelper.log(TAG, "Pref : " + PREF_PREFIX_TPATTERN_KEY + appWidgetId
				+ " | " + f);
		return f;
	}
	
	public static void fnSaveTextPatternPref(Context context, int appWidgetId,
			int frame) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putInt(PREF_PREFIX_TPATTERN_KEY + appWidgetId, frame);
		prefs.commit();
	}
	// Text Pattern Preference End
	
	// Text Size Preference Start
	public static int fnLoadTextSizePref(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		int f = prefs.getInt(PREF_PREFIX_TSIZE_KEY + appWidgetId, 24);
		LogHelper.log(TAG, "Pref : " + PREF_PREFIX_TSIZE_KEY + appWidgetId
				+ " | " + f);
		return f;
	}

	public static void fnSaveTextSizePref(Context context, int appWidgetId, int f) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putInt(PREF_PREFIX_TSIZE_KEY + appWidgetId, f);
		prefs.commit();
	}
	// Text Size Preference End
	
	// Text Color Preference Start Color.argb(88,00,00,00)
	public static int fnLoadTextColorPref(Context context, int appWidgetId) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		int f = prefs.getInt(PREF_PREFIX_TCOLOR_KEY + appWidgetId, Color.WHITE);
		LogHelper.log(TAG, "Pref : " + PREF_PREFIX_TCOLOR_KEY + appWidgetId
				+ " | " + f);
		return f;
	}

	public static void fnSaveTextColorPref(Context context, int appWidgetId, int f) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putInt(PREF_PREFIX_TCOLOR_KEY + appWidgetId, f);
		prefs.commit();
	}
	// Text Color Preference End
}
